<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class BasketsController extends AppController {

    var $name = 'Baskets';
    var $components = array(
        'Session', 'Alias', 'Upload'
    );
    var $helpers = array(
        'Editor','Published'
    );

    function index() {

        $this->set('title_for_layout', Configure::read('site_name') . ' - Giỏ trái cây');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        
        //search
        if ($search) {
            $conditions['OR'] = array(
                'title LIKE' => "%$search%",
                'note LIKE' => "%$search%",
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Basket.id' => 'DESC'
            ),
        );
        $data = $this->paginate('Basket');

        $this->set('total', $this->params['paging']['Basket']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }

    function remove() {
        $conditions = array(
            "{$this->Basket->alias}.{$this->Basket->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );
        //delete images small
        $data = $this->Basket->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('Basket.id', 'Basket.images_small')
            )
        );
        foreach ($data AS $image_file) {
            //delete it
            if (is_file(Configure::read('basket_image_thumb_absolute_path') . $image_file) && $image_file != 'no_image.gif') {
                $this->Upload->delete(Configure::read('basket_image_thumb_absolute_path') . $image_file);
            }
        }

        //delete images large
        $data = $this->Basket->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('Basket.id', 'Basket.images_large')
            )
        );
        foreach ($data AS $image_file) {
            //delete it
            if (is_file(Configure::read('basket_image_thumb_absolute_path') . $image_file) && $image_file != 'no_image.gif') {
                $this->Upload->delete(Configure::read('basket_image_thumb_absolute_path') . $image_file);
            }
        }

        if ($this->Basket->deleteAll($conditions, true)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d Bài viết", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index'));
    }

    function edit() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Basket->id = $params['cid'][0];
        } else {
            $this->Basket->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }
        if (empty($this->data)) {
            $fields = array(
                'note', 'title', 'description', 'id','status' , 'images_small','images_large', 'published', 'price', 'price_search'
            );
            //build status list
            $status_list = array(
                '1' => 'Còn Hàng',
                '2' => 'Call',
                '3' => 'Hết hàng',
            );
            $this->set('status_list', $status_list);
            $this->data = $this->Basket->read($fields);
            $check_thumbnail_exist  = is_file(Configure::read('basket_image_thumb_absolute_path') . $this->data['Basket']['images_small']);
            $check_large_thumbnail_exist  = is_file(Configure::read('basket_image_thumb_absolute_path') . $this->data['Basket']['images_large']);
            $this->set('check_thumbnail_exist', $check_thumbnail_exist);
            $this->set('check_large_thumbnail_exist', $check_large_thumbnail_exist);
            $this->set('image_url', Configure::read('basket_image_thumb_url_path') . $this->data['Basket']['images_small']);
            $this->set('large_image_url', Configure::read('basket_image_thumb_url_path') . $this->data['Basket']['images_large']);
            $this->set('description', $this->data['Basket']['description']);
        }
    }

    function add() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - thêm giỏ trái cây');
                //build status list
        $status_list = array(
            '1' => 'Còn Hàng',
            '2' => 'Call',
            '3' => 'Hết hàng',
        );
        $this->set('status_list', $status_list);
    }

    function save() {
        $data = $this->data['Basket'];

        //for upload thumbnail
        $this->Upload->setUploadpath(Configure::read('basket_image_thumb_absolute_path'));
        $this->Upload->setRules('size', Configure::read('content_image_maxsize'));
        $this->Upload->setRules('ext', Configure::read('content_image_ext'));
        $row = $this->Basket->findById($this->data['Basket']['id']);
        
        //upload small thumbnail
        if($this->data['Basket']['images_small']['name'] != ''){
            if($this->Upload->validate($this->data['Basket']['images_small'])){
                $data['images_small']   =  $this->Upload->upload($this->data['Basket']['images_small']);

                //delete it
                if(is_file(Configure::read('basket_image_thumb_absolute_path') .$row['Basket']['images_small'] ) && $row['Basket']['images_small'] != 'no_image.gif'){
                    $this->Upload->delete(Configure::read('basket_image_thumb_absolute_path') .$row['Basket']['images_small']);
                }
            }else{
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'baskets', 'action' => 'index'));
                return;
            }
        }else{
            unset ($data['images_small']);
            //delete image
            if(!empty ($this->data['Basket']['delete_image'])){
                
                if(is_file(Configure::read('basket_image_thumb_absolute_path') .$row['Basket']['images_small'] ) && $row['Basket']['images_small'] != 'no_image.gif'){
                    $this->Upload->delete(Configure::read('basket_image_thumb_absolute_path') .$row['Basket']['images_small']);
                    $data['images_small']   =   'no_image.gif';
                }
            }
        }
        
        //upload large thumbnail
         if($this->data['Basket']['images_large']['name'] != ''){
            if($this->Upload->validate($this->data['Basket']['images_large'])){
                $data['images_large']   =  $this->Upload->upload($this->data['Basket']['images_large']);
                //delete it
                if(is_file(Configure::read('basket_image_thumb_absolute_path') .$row['Basket']['images_large'] ) && $row['Basket']['images_large'] != 'no_image.gif'){
                    $this->Upload->delete(Configure::read('basket_image_thumb_absolute_path') .$row['Basket']['images_large']);
                }
            }else{
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'baskets', 'action' => 'index'));
                return;
            }
        }else{
            unset ($data['images_large']);
            //delete image
            if(!empty ($this->data['Basket']['delete_large_image'])){

                if(is_file(Configure::read('basket_image_thumb_absolute_path') .$row['Basket']['images_large'] ) && $row['Basket']['images_large'] != 'no_image.gif'){
                    $this->Upload->delete(Configure::read('basket_image_thumb_absolute_path') .$row['Basket']['images_large']);
                    $data['images_large']   =   'no_image.gif';
                }
            }
        }
        
        
        //end upload thumbnail
        $data['alias']  =   $this->Alias->alias($data['title']);

        if ($this->Basket->save($data)) {
            $this->Basket->setCache_HomeList();
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Basket->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'baskets', 'action' => 'index'));
    }

    function cancel() {
        $this->redirect(array('controller' => 'baskets', 'action' => 'index'));
    }
    function published()
    {
        $cid    =   $this->params['form']['cid'];
        $conditions =   array();
        foreach($cid AS $id){
            $conditions[]   =   array(
                'id'        =>  $id,
                'published' =>  1,
            );
        }
        
        if ($this->Basket->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Basket->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'baskets', 'action' => 'index'));
    }
    function unpublished()
    {
        $cid    =   $this->params['form']['cid'];
        $conditions =   array();
        foreach($cid AS $id){
            $conditions[]   =   array(
                'id'        =>  $id,
                'published' =>  0,
            );
        }

        if ($this->Basket->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Basket->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'baskets', 'action' => 'index'));
    }
}

?>
